package com.moleq.dialog;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.moleq.adapter.SelectionAdapter;
import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.models.ItemDiscountSetting;
import com.moleq.mgpos.activities.R;

public class SelectionStringDialog extends MoleQBaseDialog {

	private OnResultListener mOnResultListener;
	private ListView lv_dialog_list;
	private Button btn_dialog_ok,btn_dialog_cancel;
	private List<ItemDiscountSetting> listDiscount;
	private ItemDiscountSetting mItemDiscountSetting;
	private SelectionAdapter unitAdapter = null;
	private List<String> unitSource = new ArrayList<String>();
	private Context mContext;
	private MessageDialog mMessageDialog;
	private int selectedRow = 0;
	
	public void onCloseWithReturn(Object obj){
		mOnResultListener.onReturnValue(0,obj);
		dismiss();
	}
	
	public SelectionStringDialog(Context context) {
		super(context,context);
		constructor(context);
		
	}
	public SelectionStringDialog(Context context, Object base) {
		super(context,base);
		constructor(context,base);
	}
	
	public void setSource(List<ItemDiscountSetting> listDiscount,ItemDiscountSetting itemDiscount,int row){
		this.listDiscount = listDiscount;
		this.mItemDiscountSetting = itemDiscount;
		this.selectedRow = row;
	}
	private void constructor(Context context,Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}

	public SelectionStringDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}
	
	private void constructor(Context context) {
		// TODO Auto-generated method stub
		mContext = context;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.ps_dialog_string_source);
		this.setCancelable(false);
		
		lv_dialog_list = (ListView) findViewById(R.id.lv_dialog_list);
		btn_dialog_ok = (Button) findViewById(R.id.btn_source_ok);
		btn_dialog_cancel = (Button) findViewById(R.id.btn_source_cancel);
		
		btn_dialog_ok.setOnClickListener(OnClickDialogWindowListener);
		btn_dialog_cancel.setOnClickListener(OnClickDialogWindowListener);
		lv_dialog_list.setOnItemClickListener(onItemClickDialogWindowListener);
		
		unitAdapter = new SelectionAdapter(mContext, unitSource);
		lv_dialog_list.setAdapter(unitAdapter);
	}
	
	public void show(){
		super.show();	
		
		unitSource.add("%");
		unitSource.add("Amount");
		unitAdapter.ScrollToRow(0);
	}
	
	public Button.OnClickListener OnClickDialogWindowListener = new Button.OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_source_ok:
				String ds_type = unitSource.get(unitAdapter.GetSelectedIndex()).toString();
				if (ds_type.equals("%")) {
					ds_type = "P";
				}else {
					ds_type = "A";
				}
				mItemDiscountSetting.setDs_type(ds_type);
				listDiscount.set(selectedRow, mItemDiscountSetting);
				listDiscount.get(selectedRow).setEdit(true);
				onCloseWithReturn(listDiscount);
				dismiss();
				break;
			case R.id.btn_source_cancel:
				dismiss();
				break;
			}
		}
	};
	
	public OnItemClickListener onItemClickDialogWindowListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (parent.getId()) {
			case R.id.lv_dialog_list:
				unitAdapter.ScrollToRow(position);
				break;
			}
		}
	};
	
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener(){

		public void onClickYes() {
			
		}

		public void onClickNo() {
			mMessageDialog.dismiss();
		}

		public void onClickOK() {
			mMessageDialog.dismiss();
		}
	};
	
	public void ShowMessage(String text,int type,int icon){
		mMessageDialog = new MessageDialog(mContext);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
}
